﻿using CKS2320.Common.Model;
using CKS2320.Domain.DBEntity;
using CKS2320.Repository;
using CKS2320.Repository.Interface;
using CKS2320.Service.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CKS2320.Service
{
    public class VoltageWithstandService : IVoltageWithstandService
    {
        public IVoltageWithstandRepository _voltageWithstandRepository;
        public VoltageWithstandService(IVoltageWithstandRepository voltageWithstandRepository)
        {
            _voltageWithstandRepository = voltageWithstandRepository;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        public ResultReturn Add(VoltageWithstandModel voltageWithstandModel)
        {
            //AC设定电压
            float? VoltageAc = null;
            //AC最终电流  交流电
            float? CurrentAc = null;
            //DC设定电压
            float? VoltageDc = null;
            //DC设定电流  直流电
            float? CurrentDc = null;
            float? TotalLength = null;
            float? bhv = null;
            float? shv = null;
            Voltagewithstand voltageWithstandEntity = new()
            {
                Id = Guid.NewGuid(),
                StationCode = voltageWithstandModel.StationCode,
                Heart1 = voltageWithstandModel.Heart1,
                StartUpLoad = voltageWithstandModel.StartUpLoad,
                PartCode = voltageWithstandModel.PartCode,
                Heart2 = voltageWithstandModel.Heart2,
                EndUpLoad = voltageWithstandModel.EndUpLoad,
                VoltageWithstandResult = voltageWithstandModel.VoltageWithstandResult,
                CameraDetectionResult = voltageWithstandModel.CameraDetectionResult,
                VoltageWithstandResultAC = voltageWithstandModel.VoltageWithstandResultAC,
                TotalLengthResult = voltageWithstandModel.TotalLengthResult,
                RunType = voltageWithstandModel.RunType,
                Result = voltageWithstandModel.Result,
                BHVerticalityJudge = voltageWithstandModel.BHVerticalityJudge,
                SHVerticalityJudge = voltageWithstandModel.SHVerticalityJudge,
                CreatedTime = DateTime.Now,
                ModifiedTime = DateTime.Now
            };
            List<Detectiondetail> detailList = new();
            foreach (var item in voltageWithstandModel.detaillist)
            {
                detailList.Add(new Detectiondetail()
                {
                    Id = Guid.NewGuid(),
                    ParentId = voltageWithstandEntity.Id,
                    DetectedData = item.DetectedData,
                    DetectedType = item.DetectedType,
                    DetectedTypeDisplay = item.DetectedTypeDisplay,
                    Description = item.Description,
                    DetectedIndex = item.DetectedIndex,
                    DetectedStatus = item.DetectedStatus,
                    DetectedTime = item.DetectedTime,
                    PartCode = item.PartCode,
                    Plcip = item.Plcip,
                    CreatedTime = DateTime.Now,
                    ModifiedTime = DateTime.Now
                });
                VoltageAc = voltageWithstandModel.detaillist.Where(p => p.DetectedType == "ACv").FirstOrDefault()?.DetectedData;
                CurrentAc = voltageWithstandModel.detaillist.Where(p => p.DetectedType == "ACa").FirstOrDefault()?.DetectedData;
                VoltageDc = voltageWithstandModel.detaillist.Where(p => p.DetectedType == "DCv").FirstOrDefault()?.DetectedData;
                CurrentDc = voltageWithstandModel.detaillist.Where(p => p.DetectedType == "DCa").FirstOrDefault()?.DetectedData;
                TotalLength = voltageWithstandModel.detaillist.Where(p => p.DetectedType == "TL").FirstOrDefault()?.DetectedData;
                bhv = voltageWithstandModel.detaillist.Where(p => p.DetectedType == "Bpe").FirstOrDefault()?.DetectedData;
                shv = voltageWithstandModel.detaillist.Where(p => p.DetectedType == "Spe").FirstOrDefault()?.DetectedData;
            }
            Op20coredata op20CoreData = new Op20coredata()
            {
                SerialNumber = voltageWithstandModel.PartCode,
                Passageway = voltageWithstandModel.RunType,
                TestTime = DateTime.Now,
                VoltageAc = VoltageAc,
                CurrentAc = CurrentAc,
                ResultAc = voltageWithstandModel.VoltageWithstandResultAC == true ? "OK" : "NG",
                VoltageDc = VoltageDc,
                CurrentDc = CurrentDc,
                ResultDc = voltageWithstandModel.VoltageWithstandResult == true ? "OK" : "NG",
                TotalLength= TotalLength,
                TotalLengthJudge= voltageWithstandModel.TotalLengthResult == true ? "OK" : "NG",
                bhverticality = bhv,
                bhverticalityJudge = voltageWithstandModel.BHVerticalityJudge == true ? "OK" : "NG",
                shverticality = shv,
                shverticalityJudge = voltageWithstandModel.SHVerticalityJudge == true ? "OK" : "NG",
                shverticalityTotalJudge = (voltageWithstandModel.BHVerticalityJudge == true && voltageWithstandModel.SHVerticalityJudge == true)?"OK":"NG",
                finalJudge = voltageWithstandModel.Result == true ? "OK" : "NG",

            };
            return _voltageWithstandRepository.Add(voltageWithstandEntity, detailList, op20CoreData);
        }
    }
}
